package com.tpc.server.modules.core.organisation;

/**
 * @version 0.0.1
 * @author frederic.fortin
 * @modified 2010-08-17
 */

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.logging.Logger;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tpc.client.modules.core.organisation.service.OrganisationService;
import com.tpc.dao.extend.Attribut;
import com.tpc.dao.extend.Organisation;
import com.tpc.dao.extend.Tag;
import com.tpc.server.systeme.utils.SecuriteManager;
import java.sql.ResultSet;

@SuppressWarnings("serial")
public class OrganisationServiceImpl extends RemoteServiceServlet implements OrganisationService {

    private static final long serialVersionUID = 3L;
    private final static Logger objLog = Logger.getLogger(OrganisationServiceImpl.class.getName());

    public OrganisationServiceImpl() {

    }
    
    /** LoadListe **/
    public Organisation[] loadGrid() {
        try {
            Organisation[] organisations = null;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            /** CHECK IF PERMISSION ARE GOOD **/
            if (securiteManager.isAccessAllowed(Organisation.class.getSimpleName(),"READ")) {
            	String sql = "SELECT id,name, description FROM organisation WHERE life = ? ORDER BY name ";
            	String[] parameters = {securiteManager.getProperties().getProperty("ENABLE")};
                ResultSet rs = securiteManager.getDataBaseManager().executeQuery(sql,parameters);
                if (rs != null) {                	
                	int i = 0;
                    int row = 0;
                    rs.last();
                    row = rs.getRow();                   
                    organisations = new Organisation[row];
                    rs.beforeFirst();
                	while (rs.next()) {		
                		organisations[i] = new Organisation();
                		organisations[i].setId(rs.getInt("id"));
                		organisations[i].setName(rs.getString("name"));
                		organisations[i].setDescription(rs.getString("description"));
                		i++;
                	}
                }
                securiteManager.getDataBaseManager().closeConnection();           
            }
            return organisations;
        }
        catch (Exception objx) {
            objLog.warning(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    public Organisation addGrid(Organisation organisation) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"EXECUTE")) {
                //organisation = new OrganisationServiceXtend(objSecurite).addGrid(organisation);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return organisation;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    public Organisation updateGrid(Organisation organisation) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"EXECUTE")) {
                //organisation = new OrganisationServiceXtend(objSecurite).updateGrid(organisation);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return organisation;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    public Organisation removeGrid(String strId) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"EXECUTE")) {
                //success = new OrganisationServiceXtend(objSecurite).removeGrid(strId);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return null;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    public Tag[] loadListCategory() {
        try {
            Tag[] organisationCategory = null;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            if (securiteManager.isAccessAllowed(Organisation.class.getSimpleName(),"READ")) {
            	String sql = "SELECT id,name FROM tag_key,taggroup_has_key WHERE tagkeyId = id AND tag_key.life = ? AND taggroupId = ? ORDER BY name";
            	String[] parameters = {securiteManager.getProperties().getProperty("ENABLE"),securiteManager.getProperties().getProperty("GROUPID_ORGANISATION_CATEGORY")};
            	ResultSet rs = securiteManager.getDataBaseManager().executeQuery(sql,parameters);
            	if (rs != null) {
            		int i = 0;
                    int row = 0;
                    rs.last();
                    row = rs.getRow();                   
                    organisationCategory = new Tag[row];
                    rs.beforeFirst();
                	while (rs.next()) {		
                		organisationCategory[i] = new Tag();
                		organisationCategory[i].setId(rs.getInt("id"));
                		organisationCategory[i].setName(rs.getString("name"));
                		i++;
                	}
            	}
            	securiteManager.getDataBaseManager().closeConnection();
            }
            return organisationCategory;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    public Tag[] loadListType() {
    	try {
            Tag[] organisationType = null;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            if (securiteManager.isAccessAllowed(Organisation.class.getSimpleName(),"READ")) {
            	String sql = "SELECT id,name FROM tag_key,taggroup_has_key WHERE tagkeyId = id AND tag_key.life = ? AND taggroupId = ? ORDER BY name";
            	String[] parameters = {securiteManager.getProperties().getProperty("ENABLE"),securiteManager.getProperties().getProperty("GROUPID_ORGANISATION_TYPE")};
            	ResultSet rs = securiteManager.getDataBaseManager().executeQuery(sql,parameters);
            	if (rs != null) {
            		int i = 0;
                    int row = 0;
                    rs.last();
                    row = rs.getRow();                   
                    organisationType = new Tag[row];
                    rs.beforeFirst();
                	while (rs.next()) {		
                		organisationType[i] = new Tag();
                		organisationType[i].setId(rs.getInt("id"));
                		organisationType[i].setName(rs.getString("name"));
                		i++;
                	}
            	}
            	securiteManager.getDataBaseManager().closeConnection();
            }
            return organisationType;
    	 }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    /** Load One **/
    public Organisation loadForm(String strId) {
         try {
objLog.warning("id:"+strId);        	 
            Organisation organisation = null;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            if (securiteManager.isAccessAllowed(Organisation.class.getSimpleName(),"READ")) {
            	String sql = "SELECT id,name, description, created_date,modified_date FROM organisation WHERE id = ? ";
            	String[] parameters = {strId};
                ResultSet rs = securiteManager.getDataBaseManager().executeQuery(sql,parameters);
                if (rs != null) {
                	if (rs.next()) {
                		organisation = new Organisation();
                		organisation.setId(rs.getInt("id"));
                		organisation.setName(rs.getString("name"));
                		organisation.setDescription(rs.getString("description"));
                		SimpleDateFormat sdfDDmmYYYY = new SimpleDateFormat(securiteManager.getProperties().getProperty("DATE_FORMAT_VIEW"),Locale.US);
                        StringBuilder stbDateCreated = new StringBuilder("");
                        if (rs.getDate("created_date") != null) {
                            stbDateCreated = new StringBuilder(sdfDDmmYYYY.format(rs.getDate("created_date")));
                        }
                		organisation.setCreatedDateAsString(stbDateCreated.toString());
                		StringBuilder stbDateModified = new StringBuilder("");
                        if (rs.getDate("modified_date") != null) {
                            stbDateModified = new StringBuilder(sdfDDmmYYYY.format(rs.getDate("modified_date")));
                        }
                		organisation.setModifiedDateAsString(stbDateModified.toString());
                	}
                }
                securiteManager.getDataBaseManager().closeConnection();
            }
            return organisation;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }
    
    /** Add/Create **/
    public Organisation addForm(Organisation organisation) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"EXECUTE")) {
                //organisation = new OrganisationServiceXtend(objSecurite).addForm(organisation);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return organisation;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }

    /** Update/Modify **/
    public Organisation updateForm(Organisation organisation) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"EXECUTE")) {
                //organisation = new OrganisationServiceXtend(objSecurite).updateForm(organisation);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return organisation;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }
 
    /** Remove/Delete **/
    public int removeForm(String strId) {
       try {
            int success = -1;
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"EXECUTE")) {
                //success = new OrganisationServiceXtend(objSecurite).removeForm(strId);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return success;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return -2;
        }
    }

    /** LoadListe For Organisation DataSource **/
    public Organisation[] loadListComboBox(String strOrganisationTerm) {
        try {
            Organisation[] arrobjOrganisation = null;
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"READ")) {
                //arrobjOrganisation = new OrganisationServiceXtend(objSecurite).loadComboBox(strOrganisationTerm);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return arrobjOrganisation;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }
    
    /** LoadListe For Organisation DataSource **/
    public Attribut[] loadAttribut(String organisationId) {
        try {
            Attribut[] attributs = null;
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(Organisation.class.getSimpleName(),"READ")) {
                //attributs = new OrganisationServiceXtend(objSecurite).loadComboBox(strOrganisationTerm);
                objSecurite.getDataBaseManager().closeConnection();
            }
            return attributs;
        }
        catch (Exception objx) {
            objLog.info(new StringBuilder("error message: ").append(objx.getMessage()).toString());
            return null;
        }
    }
    

}